package com.wsp.controllermanagement.controller;

import io.swagger.annotations.Api;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Package: com.wsp.controllermanagement.controller
 * Description： TODO
 *
 * @Author: 巫思平
 * Copyright: Copyright (c) 2020
 * Version: 1
 */
@Controller
@RequestMapping("/local/admin")
@Api("登录/注销")
public class LoadController {
    @GetMapping("/logout")
    public String logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "index";
    }

    /**
     * 管理员登录
     */
    @PostMapping("/login")
    public String login(String adminName, String password, Model model){
        Subject subject = SecurityUtils.getSubject();
        //封装用户登录信息
        UsernamePasswordToken token = new UsernamePasswordToken(adminName,password);
        try{
            //执行登录方法
            subject.login(token);
            return "function";

        }catch (UnknownAccountException e){
            //用户名不存在
            model.addAttribute("msg","管理员名错误");
            return "index";
        }catch (IncorrectCredentialsException e){
            //密码不存在
            model.addAttribute("msg","密码错误");
            return "index";
        }
    }
}
